﻿//----------------------------------------------------------------
// Copyright (c) 文武
//----------------------------------------------------------------



using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;
using Newtonsoft.Json;
using Tools.Framework;

namespace Base.Framework
{
    public class CommonController : Controller
    {
        public bool IsLogin
        {
            get
            {
                if (RequestContext.HttpContext.Session[UserLoginKey] != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        protected static RequestContext RequestContext { set; get; }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            RequestContext = requestContext;
            base.Initialize(requestContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            ErrorHelper.ErrorLog(filterContext.Exception, ip: filterContext.RequestContext.HttpContext.Request.UserHostName + ",IP:" + filterContext.RequestContext.HttpContext.Request.UserHostAddress);
            base.OnException(filterContext);
        }


        #region 扩展


        public const string UserLoginKey = "loginusersession";//

        public new ActionResult Json(object obj, JsonRequestBehavior jrBehavior = JsonRequestBehavior.AllowGet)
        {
            Newtonsoft.Json.Converters.IsoDateTimeConverter convert = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            convert.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            var settings = new JsonSerializerSettings
            {
                //这句是解决问题的关键,也就是json.net官方给出的解决配置选项.                 
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                Converters = new List<JsonConverter>() { convert }
            };
            var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj, settings);
            return Content(str, "text/json", UTF8Encoding.UTF8);//base.Json(obj, jrBehavior);
        }


        public MessageResult ErrorResult(bool isSuccess = false, string code = "error", string msg = "操作失败！", object data = null)
        {
            return MessageResult.ErrorResult(isSuccess, code, msg, data);
        }

        public MessageResult SuccessResult(bool isSuccess = true, string code = "success", string msg = "操作成功！", object data = null)
        {
            return MessageResult.SuccessResult(isSuccess, code, msg, data);
        }

        public ActionResult MessageErrorResult(bool isSuccess = false, string code = "error", string msg = "操作失败！", object data = null)
        {
            return Json(ErrorResult(isSuccess, code, msg, data));
        }

        public ActionResult MessageSuccessResult(bool isSuccess = true, string code = "success", string msg = "操作成功！", object data = null)
        {
            return Json(SuccessResult(isSuccess, code, msg, data));
        }
        #endregion
    }
}
